package com.village.zpark.controller;

import com.github.pagehelper.PageInfo;
import com.village.zpark.common.ResultMapUtil;
import com.village.zpark.pojo.Affairs;
import com.village.zpark.service.AffairsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/affairs")
public class AffairsController {
    @Autowired
    private AffairsService affairsService;



    @RequestMapping
    public String Affairs(){
        return "/affairs";
    }

    @RequestMapping(value = "/affairsPage")
    public String affairsPage(){
        return "/affairsPage";
    }

    @RequestMapping(value = "/affairsQueryById")
    public Object AffairsQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        Affairs affairs = affairsService.findById(id);
        model.addAttribute("obj",affairs);
        return "/affairsPage";
    }

    @RequestMapping(value = "/affairsList")
    @ResponseBody
    public Object affairsList(){
        List<Affairs> affairsList = affairsService.findAll();
        return ResultMapUtil.getHashMapList(affairsList);
    }


    @RequestMapping(value = "/addaffairs")
    @ResponseBody
    public Object addaffairs(Affairs affairs){
        try {
            affairs.setEnddate(new Date());
            affairs.setStartdate(new Date());
            int i = affairsService.addAffairs(affairs);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/updateaffairs")
    @ResponseBody
    public Object updateAffairs(Affairs affairs){
        try {
            int i = affairsService.updateAffairs(affairs);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/deleteaffairs")
    @ResponseBody
    public Object deleteaffairs(Integer id){
        try {
            int i = affairsService.deleteAffairsById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/affairsQueryPage")
    @ResponseBody
    public Map<String, Object> affairsQueryPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String param) {
        PageInfo<Affairs> affairsPageInfo = affairsService.findPage(page, limit, param);
        Map<String,Object> map=new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", affairsPageInfo.getTotal());
        map.put("data",affairsPageInfo.getList());
        return map;
    }
}
